package com.hua.demo.edu.controller;


import com.hua.JwtUtils;
import com.hua.R;
import com.hua.demo.edu.entity.UcenterMember;
import com.hua.demo.edu.entity.VO.RegisterVo;
import com.hua.demo.edu.service.UcenterMemberService;
import com.hua.demo.edu.service.impl.UcenterMemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/edu/ucenter-member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    UcenterMemberService ucenterMemberService;
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){

        String token=ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

}

